package stream;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author oldliu
 * @since 1.0
 */
public class BankStream {
    public static void main(String[] args) {
        List<Bank> banks = Arrays.asList(new Bank("1", "liu1", "hefei", Math.random() * 1000), new Bank("2", "liu2", "beijing", Math.random() * 1000), new Bank("3", "liu3", "hefei", Math.random() * 1000), new Bank("3", "老刘", "anqing", Math.random() * 1000), new Bank("4", "老马", "hefei", Math.random() * 1000), new Bank("5", "小宋", "anqing", Math.random() * 1000), new Bank("6", "老吴", "hefei", Math.random() * 1000), new Bank("7", "小张", "huaibei", Math.random() * 1000));
        banks.forEach(System.out::println);
        System.out.println(">>>>>>>>>>>>>>>>");
        //:1  统计每个城市的账户人数
        Map<String, Long> c = banks.stream().collect(Collectors.groupingBy(Bank::getCity,
                Collectors.counting()));
        System.out.println(c);
        //2:统计账户的平均金额.+总额
        Double total=banks.stream().collect(Collectors.summingDouble(Bank::getMoney));
        System.out.println(total);
        Double avg = banks.stream().collect(Collectors.averagingDouble(Bank::getMoney));
        System.out.println(avg);
        //3:排序
        List<Bank> m =
                banks.stream().sorted((x, y) -> (int) (y.getMoney() -x.getMoney())).collect(Collectors.toList());

        System.out.println("；排序后:");
        m.forEach(System.out::println);

        Optional<Bank> max = banks.stream().max((x, y) -> (int) (x.getMoney() - y.getMoney()));
        System.out.println(max.isPresent()+"?"+max.get());

        //4：分类找出每个城市的账号信息，Map<城市，List<账户对象>>
        Map<String, List<Bank>> city = banks.stream().collect(Collectors.groupingBy(Bank::getCity));
        city.forEach((x,y)->{
            System.out.println(x+"的账号："+y);
        });

        long cc = banks.stream().filter(x -> x.getName().contains("刘")).count();
        System.out.println(cc);
    }
}
